// pages/game/player-score-list/player-score-list.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    currentPlayers: {
      type: Array,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    rankings: [2, 3, 1, 4],
    /** 展示分差，值为以谁为基准 */
    diffIndex: null,
    diffPoint: null,
    animationTimer: null,
  },

  lifetimes: {

  },

  pageLifetimes: {
    show() {
      this.setRankings()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setRankings() {
      const rankedPlayers = [...this.data.currentPlayers].sort((a, b) => b.point - a.point);
      const rankings = [];
      rankedPlayers.forEach((p, index) => {
        rankings[p.key] = index;
      })
      this.setData({ rankings })
    },
    handleDiff(event) {
      const diffIndex = event.target.dataset.index;
      if (diffIndex === this.data.diffIndex) {
        this.setData({ diffIndex: null, diffPoint: null })
      } else {
        const diffPoint = this.data.currentPlayers[diffIndex].point;
        this.setData({ diffIndex, diffPoint })
      }
    },
    handleDiffEnd() {
      clearTimeout(this.data.animationTimer);
      this.data.animationTimer = setTimeout(() => {
        this.setData({ diffIndex: null, diffPoint: null })
      }, 10000);
    }
  }
})